<template>
  <div>
    <el-row type="flex" justify="space-between" style="text-align: left;">
        <el-col :span="12" style="height: 50px;">
            <h2 style="margin: 0;">物品归还</h2>
        </el-col>
        <el-col :span="12" style="text-align: right;height: 50px;">
            <el-button-group>
                <el-button type="primary" @click="saveApplication">保存</el-button>
                <el-button type="primary" @click="submitApplication">提交</el-button>
            </el-button-group>
            &nbsp;&nbsp;
            <el-button type="primary" @click="reback">取消</el-button>
        </el-col>
    </el-row>

    <el-row>
        <el-col :span="20" :offset="2">
            <el-form ref="form" :model="applicationForm" label-width="80px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="申请名称"> 
                            <el-input v-model="applicationForm.applicationName" readonly style="width: 100%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请人"> 
                            <el-input v-model="applicationForm.applicationEName" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="申请部门"> 
                            <el-input v-model="applicationForm.dName" readonly></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请时间"> 
                            <el-input v-model="applicationForm.applicationTime" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="归还仓库"> 
                            <el-select v-model="applicationForm.warehouseId" placeholder="请选择" style="width: 100%" @change="getHousekeeper" :disabled="readonly">
                                  <el-option
                                  v-for="item in warehouses"
                                  :key="item.warehouseId"
                                  :label="item.warehouseName"
                                  :value="item.warehouseId">
                                  </el-option>
                              </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="仓管员"> 
                            <el-input v-model="warehouseManager" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注"> 
                            <el-input type="textarea" v-model="applicationForm.comment" :disabled="readonly"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-col>
    </el-row>

    

    <el-row>
        <el-col :span="10" :offset="1">
            <el-row>
                <el-col :span="12">
                    <h2 style="text-align: left; margin: 0;">借用列表</h2>
                </el-col>
                <el-col :span="12" style="text-align: right;">
                    <el-button type="primary" @click="addRGoods">添加</el-button>
                </el-col>
            </el-row>
            <el-table
            ref="multipleTable"
            :data="borrowGoods"
            height="350px"
            tooltip-effect="dark"
            style="width: 100%"
            stripe
            border
            highlight-current-row
            @selection-change="handleSelectionChangeB">
                  <el-table-column
                  type="selection"
                  width="55"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  type="index"
                  label="序号"
                  width="60"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  label="仓库名称"
                  align="center">
                  <template>{{this.applicationForm.warehouseName}}</template>
                  </el-table-column>
                  <el-table-column
                  prop="goodsName"
                  label="物资名称"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="brNum"
                  label="借用数量"
                  align="center">
                  </el-table-column>
            </el-table>
        </el-col>
        <el-col :span="10" :offset="2">
            <el-row>
                <el-col :span="12">
                    <h2 style="text-align: left; margin: 0;">归还列表</h2>
                </el-col>
                <el-col :span="12" style="text-align: right;">
                    <el-button type="primary" @click="delRGoods">删除</el-button>
                </el-col>
            </el-row>
            
            <el-table
                  ref="multipleTable"
                  height="350px"
                  :data="returnGoods"
                  tooltip-effect="dark"
                  style="width: 100%"
                  stripe
                  border
                  highlight-current-row
                  @selection-change="handleSelectionChangeR">
                  <el-table-column
                  type="selection"
                  align="center"
                  width="55">
                  </el-table-column>
                  <el-table-column
                  type="index"
                  label="序号"
                  align="center"
                  width="60">
                  </el-table-column>
                  <el-table-column
                  label="仓库名称" 
                  align="center">
                  <template>{{this.applicationForm.warehouseName}}</template>
                  </el-table-column>
                  <el-table-column
                  prop="goodsName"
                  label="物资名称"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="brNum"
                  label="归还数量"
                  align="center">
                  </el-table-column>
            </el-table>
        </el-col>
    </el-row>

    
  </div>
</template>

<script>
export default {
    name: 'AddorEditRApplication',
    data(){
        return{
            applicationForm:{
                applicationId:0,
                applicationName:'',
                applicationEmpNo:0,
                applicationEName:'',
                deptNo:0,
                dName:'',
                warehouseId:1,
                warehouseName:'固定资产仓库',
                applicationTime:'',
                comment:'',
                borrowType:0,
                checkType:0,
            },
            applicationGoods:[],
            
            warehouses:[],
            warehouseManager:"",    
            borrowGoods:[],
            checkedBGoods:[],
            returnGoods:[],
            checkedRGoods:[],
            user:{
                  empno:0,
                  ename:"",
                  deptno:0,
                  dname:""
            },
            readonly:false,
        }
    },
    methods:{
        getNotReturn(){
            this.borrowGoods = []
            this.$http.get("/lc/getNotReturn?empNo="+this.user.empno+"&warehouseId="+this.applicationForm.warehouseId).then(res => {
                this.borrowGoods = res.data;
            }).catch(e =>{
                console.log('未能正常获取借用物资')
            })
        },
        // getWarehouse(){
        //       this.$http.get("/lc/queryWarehouse").then(res => {
        //           this.warehouses = res.data;
        //       }).catch(e =>{ 
        //         console.log('未能正常获取仓库信息')
        //       })
        // },
        getHousekeeper(val){
            this.getNotReturn()
              this.$http.get("/lc/queryHouseKeeperByHouseId?warehouseId="+val).then(res => {
                  // console.log(res.data);
                  let houseKeeperstr = "";
                  for (let emp of res.data){
                      houseKeeperstr += emp.ename+" ";
                  }
                  this.warehouseManager = houseKeeperstr;
              }).catch(error => { 
                console.log('未能正确获取仓库管理员信息')
              })
        },
        handleSelectionChangeR(val){
            this.checkedRGoods = val;
        },
        handleSelectionChangeB(val){
            this.checkedBGoods = val;
        },
        addRGoods(){
            //向归还表添加数据
            for(let goods of this.checkedBGoods){ 
                this.returnGoods.push(goods)
            }
            //删除借用表中的数据
            for(let goods of this.checkedBGoods){ 
                for (let i = 0; i < this.borrowGoods.length; i++){
                    if (goods.goodsId == this.borrowGoods[i].goodsId){
                        this.borrowGoods.splice(i,1)
                    }
                }
            }
        },
        delRGoods(){
            for(let goods of this.checkedRGoods){ 
                this.borrowGoods.push(goods)
            }
            for(let goods of this.checkedRGoods){ 
                for (let i = 0; i < this.returnGoods.length; i++){
                    if (goods.goodsId == this.returnGoods[i].goodsId){
                        this.returnGoods.splice(i,1)
                    }
                }
            }
        },

        saveApplication(){ 
            if (this.returnGoods.length == 0){
                this.$message.error('请选择要归还的物资！')
                return
            }
            let saveData = {}
            this.applicationForm.checkType = 1
            // this.applicationForm.auditEmpNo = this.user.empno
            // this.applicationForm.checkTime = this.applicationForm.applicationTime
            saveData.application = this.applicationForm
            for (let goods of this.returnGoods){
                goods.optionType = "归还"
                goods.isReturn = goods.brid
                // goods.brid = 0
            }
            saveData.applicationGoods = this.returnGoods
            // console.log(saveData)
            this.$http.post("/lc/addApplication",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data.statu){
                    this.$message.success("保存成功")
                    
                    this.applicationForm.applicationId = res.data.applicationId
                }
            }).catch(e => {
                this.$message.error("服务器错误")
            })
        },
        submitApplication(){ 
            if (this.returnGoods.length == 0){
                this.$message.error('请选择要归还的物资！')
                return
            }
            let saveData = {}
            this.applicationForm.checkType = 2
            // this.applicationForm.auditEmpNo = this.user.empno
            // this.applicationForm.checkTime = this.applicationForm.applicationTime
            saveData.application = this.applicationForm
            for (let goods of this.returnGoods){
                goods.optionType = "归还"
                goods.isReturn = goods.brid
                // goods.brid = 0
            }
            saveData.applicationGoods = this.returnGoods
            // console.log(saveData)
            this.$http.post("/lc/addApplication",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data.statu){
                    this.$router.push({
                        path:"/returnApplicationManager",
                        query:{
                            msg:"提交成功"
                        }
                    })
                }
            }).catch(e => {
                this.$message.error("服务器错误")
            })
        },
        reback(){
            this.$router.push({
                path:"/returnApplicationManager"
            })
        },
        getDName(){
              this.$http.get("/lc/getDeptName?deptNo="+this.user.deptno).then(res => {
                this.user.dname = res.data
                this.applicationForm.dName = this.user.dname
              }).catch(error => {
                console.log("获取部门名称失败")
              })
          },
    },
    mounted(){
        // this.getWarehouse()
        this.warehouses = this.$route.query.warehouses;
        this.user.empno = localStorage.getItem("empno");
        this.user.ename = localStorage.getItem("ename");
        this.user.deptno = localStorage.getItem("deptno");
        this.getDName()
        if (this.$route.query.applicationName != null){
            this.applicationForm.applicationName = this.$route.query.applicationName;
        } else if (this.$route.query.applicationInfo != null){
            this.applicationForm = this.$route.query.applicationInfo;
            this.applicationForm.warehouseName = this.warehouses[this.applicationForm.warehouseId - 1].warehouseName
        }
        let date = new Date();
        //生成yyyy-mm-dd 格式的日期
        let dateStr = date.getFullYear() + "-"
        if (date.getMonth() < 9){
            dateStr += "0" + (date.getMonth() + 1)
        } else {
            dateStr += (date.getMonth() + 1)
        }
        if (date.getDate() < 10){
            dateStr += "-0" + date.getDate()
        } else {
            dateStr += "-" + date.getDate()
        }
        this.applicationForm.applicationTime = dateStr
        this.applicationForm.applicationEmpNo = this.user.empno
        this.applicationForm.applicationEName = this.user.ename
        this.applicationForm.deptNo = this.user.deptno
        this.applicationForm.dName = this.user.dname

        this.getHousekeeper(this.applicationForm.warehouseId)
        
    }
}
</script>

<style>


</style>